/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.transfer.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import me.zhengjie.modules.finance.domain.FinanceAd01;
import me.zhengjie.modules.finance.service.FinanceAd01Service;
import me.zhengjie.modules.logs.domain.BusinessLog;
import me.zhengjie.modules.logs.service.BusinessLogService;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.modules.transfer.domain.TransferIc58;
import me.zhengjie.tool.DateUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.transfer.service.TransferIc58Service;
import me.zhengjie.modules.transfer.domain.dto.TransferIc58QueryCriteria;
import me.zhengjie.modules.transfer.mapper.TransferIc58Mapper;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.vo.Constant;
import me.zhengjie.vo.YHConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;

import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author lzw
* @date 2025-05-26
**/
@Service
@RequiredArgsConstructor
public class TransferIc58ServiceImpl extends ServiceImpl<TransferIc58Mapper, TransferIc58> implements TransferIc58Service {

    private final TransferIc58Mapper transferIc58Mapper;
    private final FinanceAd01Service financeAd01Service;
    private final SequenceService sequenceService;
    private final BusinessLogService businessLogService;

    @Override
    public PageResult<TransferIc58> queryAll(TransferIc58QueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(transferIc58Mapper.findAll(criteria, page));
    }

    @Override
    public List<TransferIc58> queryAll(TransferIc58QueryCriteria criteria){
        return transferIc58Mapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(TransferIc58 resources) {
        transferIc58Mapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TransferIc58 resources) {
        TransferIc58 transferIc58 = getById(resources.getBaz244());
        transferIc58.copy(resources);
        transferIc58Mapper.updateById(transferIc58);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<String> ids) {
        transferIc58Mapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<TransferIc58> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TransferIc58 transferIc58 : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("险种类型", transferIc58.getAae140());
            map.put("费款所属期", transferIc58.getAae002());
            map.put("收支类型", transferIc58.getAaa039());
            map.put("业务处理类别", transferIc58.getAae710());
            map.put("本地业务处理类别", transferIc58.getBae710());
            map.put("收支金额", transferIc58.getAae719());
            map.put("单据号", transferIc58.getAae072());
            map.put("数据源表", transferIc58.getBae038());
            map.put("当事人类别", transferIc58.getAaa028());
            map.put("当事人 ID", transferIc58.getAaz010());
            map.put("当事人名称", transferIc58.getAae734());
            map.put("基金收支方式", transferIc58.getAae718());
            map.put("银行行别代码", transferIc58.getAaf200());
            map.put("地方银行行别代码", transferIc58.getAaf216());
            map.put("地方银行名称", transferIc58.getAaf217());
            map.put("银行名称", transferIc58.getAae668());
            map.put("银行户名", transferIc58.getAae009());
            map.put("银行账号", transferIc58.getAae010());
            map.put("银行所在省份", transferIc58.getAab362());
            map.put("银行所在市", transferIc58.getAaf056());
            map.put("人行支付系统行号", transferIc58.getAaf002());
            map.put("摘要", transferIc58.getAad128());
            map.put("原单据号", transferIc58.getAaz749());
            map.put("账套类别", transferIc58.getAad080());
            map.put("备注", transferIc58.getAae013());
            map.put("经办人", transferIc58.getAae011());
            map.put("经办时间", transferIc58.getAae036());
            map.put("经办机构", transferIc58.getAab359());
            map.put("个人账户基金转移额", transferIc58.getAic083());
            map.put(" aic084",  transferIc58.getAic084());
            map.put("系统跟踪流水号", transferIc58.getAaz341());
            map.put("业务日志ID", transferIc58.getAaz002());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<Map> query(Map map) {

        return transferIc58Mapper.query(map);

    }

    @Override
    public List<Map> querycount(Map map) {
        return transferIc58Mapper.querycount(map);
    }

    @Override
    public List<Map> querydetail(Map map) {
        return transferIc58Mapper.querydetail(map);
    }

    @Override
    public void save(List<Map> list) throws IOException {

        UpdateWrapper<FinanceAd01> Wrapper = new UpdateWrapper<>();
        List<String> nameList = list.stream()
                .map(map -> Convert.toStr(map.get("baz244")))
                .collect(Collectors.toList());
        Wrapper.in("baz244",nameList);
        Sequence sequence = new Sequence("aaz618");
        String aaz618 = sequenceService.nextSequence(sequence);

        Wrapper.set("aaz618",aaz618);

        financeAd01Service.update(Wrapper);


        //写日志
        Map map= new HashMap<>();
        String yyyyMMddHHmmss = DateUtil.getCurrentTime("yyyyMMddHHmmss");


        map.put("aaz002",aaz618);
        map.put("aaa121", YHConstants.AAA121_040201);
        map.put("aab001","");
        map.put("aab004","");
        map.put("aac001","");
        map.put("aac002","");
        map.put("aac003","");
        map.put("aae140",Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        map.put("aae016",Constant.AAE016_1_SHTG);
        map.put("aae011",SecurityUtils.getCurrentUsername());
        map.put("aae036",yyyyMMddHHmmss);
        map.put("aae014",SecurityUtils.getCurrentUsername());
        map.put("aae015",yyyyMMddHHmmss);
        map.put("aab359","530111");

        //7、记录日志信息
        BusinessLog businessLog = BusinessLog.singleLog(map);
        businessLogService.save(businessLog);


    }
}
